package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysRole;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.page.TableDataInfo;
import com.maimang.platform.common.service.SysRoleService;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.common.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysRoleController
 * @描述 <br>角色信息业务处理</br>
 * @日期 2019-03-28
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/role")
@Api(value = "角色信息", description = "用户角色信息业务处理")
public class SysRoleController extends BaseController {
	private String prefix = "system/role";

	@Autowired
	private SysRoleService roleService;

	@RequiresPermissions("system:role:view")
	@GetMapping()
	public String role() {
		return prefix + "/role";
	}

	@ApiOperation(value = "角色列表", notes = "返回用户角色列表数据")
	@RequiresPermissions("system:role:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysRole role) {
		startPage();
		List<SysRole> list = roleService.selectRoleList(role);
		return getDataTable(list);
	}

	@Log(title = "角色管理", businessType = BusinessType.EXPORT)
	@ApiOperation(value = "导出角色", notes = "导出角色列表信息到Excel")
	@RequiresPermissions("system:role:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(SysRole role) {
		List<SysRole> list = roleService.selectRoleList(role);
		ExcelUtil<SysRole> util = new ExcelUtil<SysRole>(SysRole.class);
		return util.exportExcel(list, "角色数据");
	}

	/**
	 * 新增角色
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存角色
	 */
	@RequiresPermissions("system:role:add")
	@ApiOperation(value = "添加角色", notes = "新增保存角色信息")
	@Log(title = "角色管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@Transactional(rollbackFor = Exception.class)
	@ResponseBody
	public Result addSave(SysRole role) {
		role.setCreateUser(ShiroUtils.getLoginName());
		ShiroUtils.clearCachedAuthorizationInfo();
		return toResult(roleService.insertRole(role));
	}

	/**
	 * 修改角色
	 */
	@GetMapping("/edit/{roleId}")
	public String edit(@PathVariable("roleId") Long roleId, ModelMap modelMap) {
		modelMap.put("role", roleService.selectRoleById(roleId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存角色
	 */
	@RequiresPermissions("system:role:edit")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改角色", notes = "修改保存角色信息")
	@PostMapping("/edit")
	@Transactional(rollbackFor = Exception.class)
	@ResponseBody
	public Result editSave(SysRole role) {
		role.setUpdateUser(ShiroUtils.getLoginName());
		ShiroUtils.clearCachedAuthorizationInfo();
		return toResult(roleService.updateRole(role));
	}

	/**
	 * 新增数据权限
	 */
	@GetMapping("/rule/{roleId}")
	public String rule(@PathVariable("roleId") Long roleId, ModelMap modelMap) {
		modelMap.put("role", roleService.selectRoleById(roleId));
		return prefix + "/rule";
	}

	/**
	 * 修改保存数据权限
	 */
	@RequiresPermissions("system:role:edit")
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改角色", notes = "修改用户角色数据权限信息")
	@PostMapping("/rule")
	@Transactional(rollbackFor = Exception.class)
	@ResponseBody
	public Result ruleSave(SysRole role) {
		role.setUpdateUser(ShiroUtils.getLoginName());
		return toResult(roleService.updateRule(role));
	}

	@RequiresPermissions("system:role:remove")
	@Log(title = "角色管理", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除角色", notes = "删除用户角色数据信息")
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		try {
			return toResult(roleService.deleteRoleByIds(ids));
		} catch (Exception e) {
			return error(e.getMessage());
		}
	}

	/**
	 * 校验角色名称
	 */
	@PostMapping("/checkRoleNameUnique")
	@ApiOperation(value = "校验角色", notes = "校验用户角色名称唯一性")
	@ResponseBody
	public String checkRoleNameUnique(SysRole role) {
		return roleService.checkRoleNameUnique(role);
	}

	/**
	 * 校验角色权限
	 */
	@PostMapping("/checkRoleKeyUnique")
	@ResponseBody
	public String checkRoleKeyUnique(SysRole role) {
		return roleService.checkRoleKeyUnique(role);
	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree")
	public String selectMenuTree() {
		return prefix + "/tree";
	}

	/**
	 * 角色状态修改
	 */
	@Log(title = "角色管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改角色状态", notes = "修改用户角色状态")
	@RequiresPermissions("system:role:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public Result changeStatus(SysRole role) {
		return toResult(roleService.changeStatus(role));
	}
}